// ==UserScript==
// @name         Highlight Fanhao
// @namespace    https://www.libredmm.com
// @version      <%= Time.now.strftime('%y%m%d.%H%M%S') %>
// @description  Highlight matched AV codes (aka Fanhao)
// @author       LibreDMM
<% ENV['TAMPERMONKEY_MATCHES'].split(',').each do |match| -%>
// @match        <%= match %>
<% end -%>
// ==/UserScript==


(function() {
    'use strict';
    document.querySelectorAll('a').forEach(function(a){
        var m = a.text.match(/<%= oneregex(
            Movie.voted_by(@user).pluck(:code) +
            Movie.bookmarked_by(@user).pluck(:code) +
            Movie.joins(:resources).pluck(:code)
        )%>/i);
        if (m) {
            var cls = []
            if (m[0].match(/<%= oneregex(Movie.upvoted_by(@user).pluck(:code)) %>/i)) {
                cls.push('upvote')
            } else if (m[0].match(/<%= oneregex(Movie.downvoted_by(@user).pluck(:code)) %>/i)) {
                cls.push('downvote')
            } else if (m[0].match(/<%= oneregex(Movie.bookmarked_by(@user).pluck(:code)) %>/i)) {
                cls.push('bookmark')
            }
            <% Resource.distinct.pluck(:tags).flatten.sort.uniq.each do |tag| %>
            if (m[0].match(/<%= oneregex(Movie.with_resource_tag(tag).pluck(:code)) %>/i)) {
                cls.push('<%= tag %>')
            }
            <% end %>
            var mark = `<mark class="${cls.join(' ')}">${m[0]}</mark>`;
            a.innerHTML = a.innerHTML.replace(m[0], mark);
        }
    });
})();